﻿String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(document).ready(function() {
    $("[id^='ctl00_MainContent_Control_YSBAXFSS1_cbl'][type='checkbox']").each(function() {
        $(this).bind("click", function() {
            var a = $(this).attr("id").toString();
            var b = $("input[id^='ctl00_MainContent_Control_YSBAXFSS1_cb" + a.substr(a.length - 6, 4) + "'][type='checkbox']");
            if ($(this).attr("checked") != "") {
                b.attr("checked", "checked");
            }
        });
    });
    $("[id^='ctl00_MainContent_Control_YSBAXFSS1_tb'][type='text']").each(function() {
        $(this).bind("blur", function() {
            var a = $(this).attr("id").toString();
            var b = $("input[id^='ctl00_MainContent_Control_YSBAXFSS1_cb" + a.substr(a.length - 4, 4) + "'][type='checkbox']");
            if ($(this).val() != "") {
                b.attr("checked", "checked");
            }
        });
    });

    $("[id^='ctl00_MainContent_cblistSyxz'][type='checkbox']").each(function() {
        $(this).bind("click", function() {
            var s = "";
            $("[id^='ctl00_MainContent_cblistSyxz'][type='checkbox']").each(function() {
                var b = $(this).attr("id").toString();
                if ($(this).attr("checked") != "") {
                    var c = $("label[for='" + b + "']").text();
                    s = s + c + ",";
                }
            });

            $("#ctl00_MainContent_txtSyxz").val(s.substring(0, s.length - 1));
        });
    });

    $("#ctl00_MainContent_txtSyxz").bind("blur", function() {
        var a = $(this).val();
        //alert(a);
        $("[id^='ctl00_MainContent_cblistSyxz'][type='checkbox']").each(function() {
            var b = $(this).attr("id").toString();
            var c = $("label[for='" + b + "']").text();
            if (a != "") {
                if (a.indexOf(c) != -1) {
                    $(this).attr("checked", "checked");
                }
                else {
                    $(this).removeAttr("checked");
                }
            }
            else {
                $(this).removeAttr("checked");
            }
        });
    });

    $("#ctl00_MainContent_WWBADR").bind("click", function() {
        var Beian_BH = $("#ctl00_MainContent_txtBeian_BH").val();
        popWin('BASLWWSelectListPage.aspx?Beian_No=' + Beian_BH + "&Beian_type=2", '备案数据导入');
    });

    //--------------------建筑处理------------------
    $("#btnAddJZ1").bind("click", function() {

        var front = "";
        var last = "";
        var max = 0;
        var count = 0;
        //获取最大id
        $("tr[id^='jztr']").each(function() {
            //if ($(this).css("display") != "none") {
            front = $(this).attr("id");
            last = front.toString().substr(4, front.length - 4);
            if (Number(last) > max) {
                max = last;
                // alert(max);
            }
            count++;
            //}
        });
        $("a[id*='btnDelJZ']").show();
        var newid = Number(max) + 1;
        var lastNumber = count + 1;
        $("#tableJZ tr:nth-child(" + lastNumber + ")").after($("#jztr" + max).clone().attr("id", "jztr" + newid));
        $("#jztr" + newid + " input").each(function() {
            $(this).val(""); //清空数据
            $(this).css("background-color", "");
            //alert($(this).attr("id"));
        })
        $("#jztr" + newid + " select").each(function() {
            $(this).get(0).selectedIndex = 0; //初始
            $(this).css("background-color", "");
        })
        $("#jztr" + newid + " a").each(function() {
            var count = 0;
            $(this).attr("id", "btnDelJZ" + newid);
            $(this).bind("click", function() {
                $("tr[id^='jztr']").each(function() {
                    //if ($(this).css("display") != "none") {
                    count++;
                    //}
                });
                if (count > 1) {
                    if (count == 2) {
                        $("a[id*='btnDelJZ']").each(function() {
                            $(this).hide();
                        })
                    }
                    $("#jztr" + newid).remove();
                }
            })
        })
    });
    $("a[id*='btnDelJZ']").each(function() {
        $(this).bind("click", function() {
            var count = 0;
            $("tr[id^='jztr']").each(function() {
                //if ($(this).css("display") != "none") {
                count++;
                //}
            });
            if (count > 1) {
                if (count == 2) {
                    $("a[id*='btnDelJZ']").each(function() {
                        $(this).hide();
                    })
                }
                $(this).parent().parent().remove();
            }
        });
    });
    $("#ctl00_MainContent_txtBeian_BH").bind("blur", function() {
        if ($("#ctl00_MainContent_cbjbbg").length > 0 && $("#ctl00_MainContent_cbjbbg").attr("checked") != "") {
            $("#spanBeian_BH").css("display", "none");
        }
        else if ($(this).val().Trim() != "" && $(this).val().Trim() != $("#ctl00_MainContent_lblBeian_BH").text()) {
            $.getJSON("../../XTWHPage/CYDWPage/AjaxTreeList.ashx", {
                'BABH': $(this).val()
            }, function(json) {
                $.each(json, function(i, n) {
                    if (n.toString() == "false") {
                        $("#spanBeian_BH").css("display", "");
                        //$("#ctl00_MainContent_WWBADR").css("display", "none");
                    }
                    else {
                        $("#spanBeian_BH").css("display", "none");
                        //$("#ctl00_MainContent_WWBADR").css("display", "");
                    }
                });
            });
        }
    });
});

function GetEventDW() {
    $("#ctl00_MainContent_HiddenField2").val("DW");
}
function GetEventCG() {
    $("#ctl00_MainContent_HiddenField2").val("CG");
}
function GetEventDC() {
    $("#ctl00_MainContent_HiddenField2").val("DC");
}
//建筑验证
function jzdata() {

    var jzdata = "";
    var yanzheng = true;
    var xiaoshu = ['txtJzgd', 'txtJzmj'];
    $("tr[id^='jztr']").each(function() {
        if ($(this).css("display") != "none") {
            $(this).children("td").each(function() {
                $(this).children("input").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("txt"), id.length);
                    var bl = true;
                    switch (subid) {
                        case "txtJzmc":
                        case "txtJzcs":
                        case "txtJzgd":
                        case "txtJzmj":
                            if ($(this).val().toString().Trim() == "") {
                                $(this).css("background-color", "plum");
                                yanzheng = false;
                                bl = false;
                            }
                            else {
                                jzdata += subid + "★" + $(this).val() + "☆";
                                $(this).css("background-color", "");
                            }
                            break;
                    }
                    if (bl == true) {
                        switch (subid) {
                            case "txtJzgd":
                                var mode = /^\d{1,8}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            case "txtJzmj":
                                var mode = /^\d{1,7}(\.\d{1,2})?$/;
                                if (!mode.test($(this).val())) {
                                    $(this).css("background-color", "plum");
                                    yanzheng = false;
                                }
                                else {
                                    jzdata += subid + "★" + $(this).val() + "☆";
                                    $(this).css("background-color", "");
                                }
                                break;
                            default:
                                jzdata += subid + "★" + $(this).val() + "☆";
                                break;
                        }
                    }
                })
                $(this).children("select").each(function() {
                    var id = $(this).attr("id").toString().Trim();
                    var subid = id.substr(id.search("drop"), id.length);
                    if (subid == "dropNhdj") {
                        if ($(this).get(0).selectedIndex == "0") {
                            yanzheng = false;
                            $(this).css("background-color", "plum");
                        }
                        else {
                            $(this).css("background-color", "");
                            $(this).children("option").each(function() {
                                if ($(this).attr("selected") != "" && $(this).attr("value") != "#") {
                                    jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                                }
                            })
                        }
                    }
                    else {
                        $(this).children("option").each(function() {
                            if ($(this).attr("selected") != "") {
                                if ($(this).attr("value") != "#") {
                                    jzdata += subid + "★" + $(this).attr("value") + "◇" + $(this).text() + "☆";
                                }
                                else {
                                    jzdata += subid + "★◇☆";
                                }
                            }
                        })
                    }
                });
            });
        }
    });
    if (jzdata.length > 0) {
        jzdata = jzdata.substr(0, jzdata.length - 1);
    }
    if (yanzheng == false) {
        alert("建筑数据格式不正确");
    }
    $("#ctl00_MainContent_HiddenField1").val(jzdata);
    return yanzheng;
}

//保存数据并验证
function GetEventSave() {
    var yanzheng = true;
    if ($("#ctl00_MainContent_dropDw").size() > 0) {
        if ($("#ctl00_MainContent_dropDw").get(0).selectedIndex == 0) {
            yanzheng = false;
            alert("请选择管辖单位");
        }
    }
    if (yanzheng == true) {
        if ($("#spanBeian_BH").css("display") != "none") {
            alert("备案号重复请重新录入");
            yanzheng = false;
        }
    }
    if (yanzheng == true) {
        yanzheng = jzdata(); //验建筑
    }
    $("#ctl00_MainContent_HiddenField2").val("Save");
    return yanzheng;
}

function DelDC() {
    if (confirm('你确定要删除最后一个堆场')) {
        GetEventDC();
        return true;
    }
    else
        return false;
}
function DelCG() {
    if (confirm('你确定要删除最后一个储罐')) {
        GetEventCG();
        return true;
    }
    else
        return false;
}
function DelDW() {
    if (confirm('你确定要删除该单位')) {
        GetEventDW();
        return true;
    }
    else
        return false;
}

function popWin(selectAction, Title) {
    setTimeout(
                      function() {
                          ymPrompt.win({ message: selectAction + "&DWMC=" + $("#ctl00_MainContent_txtJSDW").val(),
                              width: 760,
                              height: 500,
                              title: Title,
                              handler: null,
                              maxBtn: true,
                              minBtn: true,
                              iframe: true
                          });
                      }, 50);
    return false;
}

function removeDW() {
    //$("#ctl00_MainContent_txtJSDW").val("");
    $("#ctl00_MainContent_HidDWID").val("");
    $("#ctl00_MainContent_selectDW").html("单位关联");
    $("#ctl00_MainContent_removeDW").css("display", "none");
    $("#ctl00_MainContent_selectTable").css("display", "");
    $("#ctl00_MainContent_selectTable1").css("display", "none");
    return false;
}

function Getbh(p1, p2) {
    var obj = document.getElementById(p2);
    var WinElementPos = getWinElementPos(obj);
    var val = document.getElementById(p1);
    val.style.display = "inline";
    val.style.left = (parseInt(WinElementPos.x) - 32).toString() + "px";
    val.style.top = (parseInt(WinElementPos.y) - 107).toString() + "px";
}
function yincan(p1) {
    var val = document.getElementById(p1);
    val.style.display = "none";
}
function Getbh1(p1, p2) {
    var obj = document.getElementById(p2);
    var WinElementPos = getWinElementPos(obj);
    var val = document.getElementById(p1);
    val.style.display = "inline";
    val.style.left = (parseInt(WinElementPos.x) - 32).toString() + "px";
    val.style.top = (parseInt(WinElementPos.y) - 92).toString() + "px";
}
function yincan1(p1) {
    var val = document.getElementById(p1);
    val.style.display = "none";
}